import Vue from 'vue'

export let ideState = Vue.observable({ 
  current: 0, 
  list: [{ 
    name: 'Tom', 
    idx: 0, 
    content: 'xml' 
  }] 
})


export const ideStore = {
  add(item) {
    ideState.list.push(item)
    ideState.current = ideState.list.length
    return ideStore
  },
  delete(idx) {
    ideState.list = ideState.list.filter(item => item.idx !== idx)
    ideState.current -= 1
    return ideStore
  },
  update(idx, uitem) {
    let position = ideState.list.reduce((pos, item, index) => {
      if (item.idx === uitem.idx) {
        return index;
      }
      return pos;
    }, -1)

    ideState.list.splice(position, 1, uitem)
    return ideStore
  },
  get(idx) {
    if (idx === undefined) {
      return ideState
    }
    return ideState.list.find(item => item.idx === idx)
  }
}

export default ideStore